package leetcode;

/**
 * 动态规划：两个字符创的删除操作
 */
public class Solution67 {
    public int minDistance(String word1, String word2) {
        //获取字符串长度
        int length1 = word1.length();
        int length2 = word2.length();
        //创建dp数组
        int[][] dp = new int[length1 + 1][length2 + 1];
        //for循环遍历
        for(int i = 1;i <= length1;i++){
            for(int j = 1;j <= length2;j++){
                if(word1.charAt(i-1) == word2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else{
                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return length1 + length2 - dp[length1][length2] * 2;
    }
}
